﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;

namespace JewererBasic
{
    public class Waste_Report_BO
    {
        public static WASTE_REPORT CreateReport()
        {
            using (TransactionScope scope=new TransactionScope())
            {
                MSQDBEntities db = new MSQDBEntities();                

                /* Add Waste Report*/
                WASTE_REPORT waste_report = new WASTE_REPORT();
                waste_report.DateMake = DateTime.Now;                
                waste_report.Total_Weight = Math.Round(waste_report.WASTE_REPORT_DETAIL.Sum(x => x.Waste_Weight), 3);
                db.WASTE_REPORT.Add(waste_report);                
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    return null;
                }

                // Generate Report Detail
                var worker_waste = db.WORKER_WASTE.ToList();
                foreach (var item in worker_waste)
                {
                    WASTE_REPORT_DETAIL detail = new WASTE_REPORT_DETAIL();
                    detail.Id_Waste_Report = waste_report.Id;
                    detail.Staff_Id = item.Worker_Id;
                    detail.Waste_Weight = item.Total_Weight;
                    db.WASTE_REPORT_DETAIL.Add(detail);

                    waste_report.Total_Staff += 1;
                    waste_report.Total_Weight += detail.Waste_Weight;
                    // Remove old information
                    db.WORKER_WASTE.Remove(item);
                }

                waste_report.Total_Weight=Math.Round(waste_report.Total_Weight,3);
                try
                {
                    db.SaveChanges();
                }
                catch (Exception ex)
                {
                    return null;
                }
                scope.Complete();
                return db.WASTE_REPORT.Find(waste_report.Id);
            }
        }

        public static List<WASTE_REPORT> GetAllReport()
        {
            MSQDBEntities db = new MSQDBEntities();
            return db.WASTE_REPORT.ToList();
        }

        public static WASTE_REPORT GetReport(int id)
        {
            MSQDBEntities db = new MSQDBEntities();
            return db.WASTE_REPORT.Find(id);
        }

        public static void Clear_Worker_Waste_Table()
        {
            MSQDBEntities db = new MSQDBEntities();
            while (db.WORKER_WASTE.Count() > 0)
            {
                db.WORKER_WASTE.Remove(db.WORKER_WASTE.First());
            }
            db.SaveChanges();
        }

        public static bool Update_Worker_Waste(int worker_id, BAG_PROCESS_HISTORY bag_history)
        {
            MSQDBEntities db = new MSQDBEntities();
            var model = (from d in db.WORKER_WASTE
                         where d.Worker_Id == worker_id
                         select d).FirstOrDefault();
            if (model == null)
            {
                model = new WORKER_WASTE();
                model.Details="";
                model.Total_Weight = 0;
                model.Worker_Id = worker_id;
                model.FromDay = DateTime.Now;
                db.WORKER_WASTE.Add(model);
            }                        
            model.Total_Weight = Math.Round(model.Total_Weight+CaculateWaste(bag_history),3);
            db.SaveChanges();
            return true;
        }

        public static bool Update_Worker_Waste(int worker_id, COMPONENT_ORDER_DETAIL com_order_deatail)
        {
            MSQDBEntities db = new MSQDBEntities();
            var model = (from d in db.WORKER_WASTE
                         where d.Worker_Id == worker_id
                         select d).FirstOrDefault();
            if (model == null)
            {
                model = new WORKER_WASTE();
                model.Details = "";
                model.Total_Weight = 0;
                model.Worker_Id = worker_id;
                model.FromDay = DateTime.Now;
                db.WORKER_WASTE.Add(model);
            }

            model.Total_Weight = Math.Round(model.Total_Weight + CaculateWaste(com_order_deatail), 3);
            db.SaveChanges();
            //model.Details+=;
            return true;
        }

        public static bool Update_Worker_Waste(int worker_id, COMPONENT_EXPORTSTAFFDETAIL com_export_staff)
        {
            MSQDBEntities db = new MSQDBEntities();
            var model = (from d in db.WORKER_WASTE
                         where d.Worker_Id == worker_id
                         select d).FirstOrDefault();
            if (model == null)
            {
                model = new WORKER_WASTE();
                model.Details = "";
                model.Total_Weight = 0;
                model.Worker_Id = worker_id;
                model.FromDay = DateTime.Now;
                db.WORKER_WASTE.Add(model);
            }

            model.Total_Weight = Math.Round(model.Total_Weight + com_export_staff.Weight-com_export_staff.ReturnWeight-com_export_staff.RejectWeight, 3);
            db.SaveChanges();
            //model.Details+=;
            return true;
        }

        public static bool Update_Worker_Waste(int worker_id, ORDER_TREE_DETAIL order_tree_detail)
        {
            MSQDBEntities db = new MSQDBEntities();
            var model = (from d in db.WORKER_WASTE
                         where d.Worker_Id == worker_id
                         select d).FirstOrDefault();
            if (model == null)
            {
                model = new WORKER_WASTE();
                model.Details = "";
                model.Total_Weight = 0;
                model.Worker_Id = worker_id;
                model.FromDay = DateTime.Now;
                db.WORKER_WASTE.Add(model);
            }

            model.Total_Weight = Math.Round(model.Total_Weight + CaculateWaste(order_tree_detail), 3);
            db.SaveChanges();
            return true;
        }

        public static double CaculateWaste(ORDER_TREE_DETAIL order_tree_detail)
        {
            return order_tree_detail.ExportWeight - order_tree_detail.RejectWeight - order_tree_detail.ReturnWeight;
        }

        public static double CaculateWaste(BAG_PROCESS_HISTORY bag)
        {
            if (bag.WeigthIn != null)
            {
                if (bag.RejectWeight != null)
                    return bag.WeigthIn.Value - bag.WeightOut.Value - bag.RejectWeight.Value;
                return bag.WeigthIn.Value - bag.WeightOut.Value;
            }
            return 0;
        }

        public static double CaculateWaste(COMPONENT_ORDER_DETAIL detail)
        {
            // caculate total_export_weight
            double total_export_weight = detail.COMPONENT_ORDER_COM_MOLECULE_EXPORT.Sum(x => x.Export_Weight);
            if(detail.Dust!=null)
                return total_export_weight - detail.Return_Weight - (double)detail.Dust;
            return total_export_weight - detail.Return_Weight;
        }
    }
}